<?php
//后台控制器
class AdminController extends Controller {
    private $manager = null;
    public function __construct() {
        parent::__construct();
        $this->viewFolder = 'public/';
        $this->manager = new ManagerModel();
    }
    //后台初始页面
    public function index() {
        if(isset($_SESSION['admin'])){
            $this->tpl->assign('admin',$_SESSION['admin']);
            $this->tpl->display(SMARTY_ADMIN.$this->viewFolder.'admin.tpl');
        } else {
            $this->redirect->success('?c=admin&m=login');
        }

    }
    /*
     * ajax 方式验证后台登陆验证码是否正确
     */
    public function checkCode() {
        if(isset($_GET['code'])){
            if(strtolower($_GET['code']) == $_SESSION['code']) echo 'success';
        }
    }
    //起始页面
    public function main() {
        $this->tpl->display(SMARTY_ADMIN.$this->viewFolder.'main.tpl');
    }
    //后台登陆
    public function login() {
        if(isset($_POST['user'])){
            if(!!$result = $this->manager->login()){
                if($result->passwd == sha1($_POST['passwd'])){
                    $login = $this->manager->findLogin();
                    $_SESSION['admin']['user'] = $login[0]->user;
                    $_SESSION['admin']['level_name'] = $login[0]->level_name;
                    $this->manager->updateLogin();
                    echo 'success'; //用户名存在，密码正确
                }else{
                    echo 'warning'; //用户名存在,密码不正确
                }
            }else{
                echo 'error';       //用户名不存在
            }
            exit();
        }
        $this->tpl->display(SMARTY_ADMIN.$this->viewFolder.'login.tpl');
    }
    //后台退出
    public function logout() {
        if(isset($_SESSION['admin'])){
            session_destroy();
            $this->redirect->success('?c=admin&m=login');
        }
    }
}